var config = require(process.cwd() + '/rightfe')
if (!config.css) return

var gulp = require('gulp')
var gulpif = require('gulp-if')
var browserSync = require('browser-sync')
var sass = require('gulp-sass')
var sourcemaps = require('gulp-sourcemaps')
  //var clean = require('gulp-clean')
var handleErrors = require('../utils/handleErrors')
var autoprefixer = require('gulp-autoprefixer')
var cssnano = require('gulp-cssnano')
var path = require('path')

var paths = {
  src: path.join(config.root.src, config.css.src, '/**/entry.{' + config.css
    .extensions + '}'),
  dest: path.join(config.root.dest, config.css.dest)
};


var cssTask = function(publish) {
  return gulp.src(paths.src)
    .pipe(gulpif(!publish, sourcemaps.init()))
    .pipe(sass())
    .on('error', handleErrors)
    .pipe(autoprefixer(config.css.autoprefixer))
    //压缩
    .pipe(gulpif(publish, cssnano({
      autoprefixer: false
    })))
    .pipe(gulpif(!publish, sourcemaps.write()))
    .pipe(gulp.dest('./' + paths.dest))
    .pipe(browserSync.stream())

};

module.exports = cssTask;
